#!/bin/bash
#this script is to export all verilog files hierarchially to a single file 
#(in order to collaborate with QFLOW RTL2GDS Flow)
#USAGE: bash hierarchy_export.sh $(TARGET_FILE) $(HIERARCHY_REPORT)


cat $2 |xargs cat |sed -n '/\#PREPROCESS_START/,/\#PREPROCESS_END/p'>./temp/PREPROCESSOR.sh
bash ./temp/PREPROCESSOR.sh

ABS_PATH=$(pwd)
    cat $2 |sed '/^[  ]*$/d'|sed '/^\s*$/d' >/tmp/temp.rpt
echo  -e '\n' >/tmp/temp.v
tac /tmp/temp.rpt | while read line
	do
        echo $line
        cat $line >>/tmp/temp.v
        echo  -e '\n' >>/tmp/temp.v
        echo  -e '\n' >>/tmp/temp.v
	done
grep '`include' /tmp/temp.v >/tmp/includes.v
cat /tmp/includes.v|grep '`include "'|sort -k2n |uniq >$1 #STILL HAVE PROBLEM
echo -e '\n' >>$1
#export verilog with no commentssed '/\/\*/,/\*\//d'|sed '/^[  ]*$/d'|sed '/^\s*$/d'|
cat /tmp/temp.v |sed 's/`include.*$//g' |sed 's/\/\/.*$//g'>>$1


